package org.hd.lineloss.fill.controller;

import cn.dev33.satoken.util.SaResult;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.hd.lineloss.fill.service.LineLossFillService;
import org.hd.utils.exportExcelUtil.ExportExcel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;

@RestController
@RequestMapping("/system/lineLossFill/")
public class LineLossFillController {

    @Resource
    LineLossFillService lineLossFillService;

    /***
     * 插入填报的数据
     * @param map
     * @return
     */
    @PostMapping("updateFillTemp")
     public SaResult updateFillTemp( @RequestBody Map<String,String> map){
        //是否填报成功
        boolean flag = lineLossFillService.fillReport(
                map.get("insertDate"),
                map.get("username"),
                map.get("fillConfig"),
                map.get("insertTime")
                );
        if(flag){
            return new SaResult(200,"提交成功","");
        }
        return new SaResult(500,"提交失败","");
    }

    /***
     * 如果数据库中没有当月已经填报的数据，就返回模板，如果有的话就返回当月的已填报的数据
     *  将数据作为数据流传输
     * @return
     */
    @PostMapping("getReport")
    public SaResult getReport(
            @RequestBody Map<String,String> prams,
            HttpServletResponse response
    ) throws IOException {
        //查询到的结果,使用数据流方式传到前端，如果传字符串的话会出现截断的情况
        Map<String,String> map = lineLossFillService.getFillReport(prams.get("date"), prams.get("username"));
        if(null != map){
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Length", "" + map.get("option").getBytes().length);

            ServletOutputStream outputStream = response.getOutputStream();

            outputStream.write(map.get("option").getBytes());
            outputStream.flush();
            outputStream.close();
            return null;
        }
        return new SaResult(500,"查询失败","");
    }

//    获取填报的时间
    @GetMapping("getFillDate")
    public SaResult getFillDate(
            @RequestParam String username,
            @RequestParam String month
    ){
        String insertTime = lineLossFillService.getFillDate(username,month);
        if(null != insertTime){
            return new SaResult(200,"获取提交时间成功",insertTime);
        }
        return new SaResult(500,"获取提交时间失败","");
    }

//    检查一下还有谁没填报
    @GetMapping("checkUnFill")
    public SaResult checkUnFill(
            @RequestParam("month") String month
    ){
        return new SaResult(200,"检查成功",lineLossFillService.checkUnFill(month));
    }

    /***
     * 下载excel
     * @param map
     * @return
     */
    @PostMapping("downloadExcel")
    public SaResult downLoadExcel(
            @RequestBody Map<String,String> map,
            HttpServletResponse response
    ) throws IOException {

        XSSFWorkbook excel = lineLossFillService.exportExcel(map.get("excelOption"));
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("content-disposition",
                "attachment;filename=" + URLEncoder.encode("网损月报.xlsx", "UTF-8"));
        // 允许浏览器访问header中的FileName
        response.setHeader("Access-Control-Expose-Headers", "FileName");
        // 设置FileName，转码防止中文乱码
        response.setHeader("FileName", URLEncoder.encode("网损月报.xlsx", "UTF-8"));

        ServletOutputStream outputStream = response.getOutputStream();
        excel.write(outputStream);
        outputStream.flush();
        outputStream.close();
        return null;
    }

    @PostMapping("test")
    public SaResult test(@RequestParam(value = "file1",required = false) MultipartFile file1){
        return new SaResult(200,"ok",null);
    }


}
